package com.zyj.delayqueue.schedule;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorServiceExample {
    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(4);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            //java.util.concurrent.ScheduledExecutorService.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit)
            //这个方法就起到了延时队列的功能
            scheduledExecutorService.schedule(new Runnable() {
                @Override
                public void run() {
                    //业务逻辑写这
                    System.out.println(finalI);
                }
            }, i, TimeUnit.SECONDS);
        }
        TimeUnit.DAYS.sleep(1);
    }
}
